package org.willthink.controller.freeboard;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.willthink.common.PageUtil;
import org.willthink.common.PagingProcessUtil;
import org.willthink.service.freeboard.FreeBoardService;

@Controller
@RequestMapping("/freeboard")
public class FreeBoardController {

	private Logger logger = LoggerFactory.getLogger(getClass());
	private FreeBoardService service;
    
	public void setService(FreeBoardService service) {
		this.service = service;
	}
		
	@RequestMapping(value="/freeboardList.think")
	public ModelAndView list(HttpServletRequest request , HttpServletResponse response) throws Exception{

		ModelAndView mav = new ModelAndView("freeboard/freeboardList");
		HashMap inData = new HashMap();
//		String contextPath = request.getRequestURI();
//		logger.debug("contextPath : "+contextPath );
		PageUtil pageUtil = new PageUtil();
		inData.put("PTYPE", "C");
		List<HashMap> countList = service.list(inData);
		logger.info("MMSController resultList :::"+ countList.size());
		HashMap resultMap= new HashMap();
		
		//총 카운트
		int totCnt = 0;
			for( int i=0;i<countList.size();i++){
				resultMap = countList.get(0);
				System.out.println(resultMap);
				totCnt = Integer.parseInt(resultMap.get("TOT_CNT").toString());		
				logger.debug("MMSController totCnt :::"+ totCnt);
			}
			
		// 페이징 
		String cpage = (String)request.getParameter("cpage") ==null  ? "0" : (String)request.getParameter("cpage");
		int page = Integer.parseInt(cpage);
		int blockSize = 10;
		int pageSize =10;
			logger.debug("MMSController cpage :::"+ cpage+"/pageSize ::: "+ pageSize + "/blockSize :::"+blockSize);
		inData.put("page", page);
		inData.put("blockSize", blockSize);
		inData.put("pageSize", pageSize);
		
		//리스트 
		inData.put("PTYPE", "L");
		List<HashMap> resultList = null;
		if( totCnt>0){
			resultList = (List)service.list(inData);
		}
		int cPage=0;
		if(page ==0){cPage=1;}else{cPage =page;}
		logger.debug("freeBoard paging  "+" / totCnt : "+ totCnt+" / cPage : "+cPage+" / pageSize : "+pageSize+" / blockSize" );
		String pageNavigation = pageUtil.NavigationUtil(totCnt, cPage, pageSize, blockSize);
		
		//검색 조건 :일자, 작성자, 내용
		mav.addObject("resultList", resultList);
		mav.addObject("totCnt", totCnt);
		mav.addObject("page", page);
		mav.addObject("blockSize", blockSize);
		mav.addObject("pageSize", pageSize);
		mav.addObject("pageNavigation", pageNavigation);
		
		return mav;
	}	
}
